package networking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;

public class MyServer {

	public static void main(String[] args) throws IOException {
		ServerSocket ss = null;
		int portNumber = 1234;
		ss = new ServerSocket(portNumber);
		Integer result;

		while (true) {
			Socket socket = ss.accept();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream()));
			String message = reader.readLine();
			message = message.replaceAll(",", " ");
			List<String> elements = stringToList(message);
			result = Integer.parseInt(elements.get(0))
					+ Integer.parseInt(elements.get(1));
			writer.write(result.toString());
			writer.flush();
			if (message.contains("done")) {
				socket.close();
				ss.close();
				break;
			}
		}

	}

	public static List<String> stringToList(String string) {
		List<String> elements;
		elements = Arrays.asList(string.split(" "));
		return elements;
	}
}
